<template>
  <!-- 新增录入画面对话框-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="editForm" :model="form" :rules="rules" label-width="110px">
      <!--交易础信息-->
      <el-card class="form-card txnInfo-card">
        <div slot="header" class="label-header-msg">
          <span>交易基础信息</span>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务类型" prop="bizTypeCode">
              <el-select v-model="form.bizTypeCode" placeholder="业务类型" @change="bizTypeCodeChange">
                <el-option
                  v-for="dict in dictMap.BIZ_TYPE_122"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务种类" prop="bizCtgyCode">
              <el-select v-model="form.bizCtgyCode" placeholder="业务种类" @change="bizCtgyCodeChange">
                <el-option
                  v-for="dict in busTypeList"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
              <el-form-item label="业务优先级" prop="bizPriority">
                <el-select v-model="form.bizPriority" placeholder="业务优先级">
                  <el-option
                    v-for="dict in dictMap.PRIORITY3CODE"
                    :key="dict.value"
                    :label="`${dict.value}-${dict.text}`"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row> -->

          <!-- <el-row>
            <el-col :span="8">
              <el-form-item label="交易渠道" prop="transSource">
                <el-input
                  v-model="form.transSource"
                  :disabled="true"
                  placeholder="柜面交易"
                  style="width:195px"
                />
              </el-form-item>
            </el-col>-->
          <el-col :span="12">
            <el-form-item label="记账标志" prop="cbflag">
              <el-select v-model="form.cbflag" placeholder="记账标志">
                <el-option
                  v-for="dict in dictMap.CBFLAG"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户类型" prop="custflag1">
              <el-select v-model="form.custflag1" placeholder="账户类型">
                <el-option
                  v-for="dict in dictMap.CUST_FLAG1"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-row>
          <el-col :span="8">
            <el-form-item label="交易币种" prop="transCNY">
              <el-input v-model="form.transCNY" placeholder="人民币-CNY" :disabled="true" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="交易金额" prop="amount">
              <ht-amount-input
                v-model="form.amount"
                placeholder="交易金额"
                style="width: 100%"
                @keyup.enter.native="checkAmountLimit()"
              ></ht-amount-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="大写金额" prop="bAmount">
              <ht-amount-upper :inputValue="form.amount" placeholder="交易金额" style="width: 100%"></ht-amount-upper>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-card>

      <!--付款人信息-->
      <el-card class="form-card payerInfo-card">
        <div slot="header" class="label-header-msg">
          <span>付款人信息</span>
        </div>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="付款人账号" prop="payerActno">
              <acct-no-input
                v-model="form.payerActno"
                placeholder="付款人账号"
                @keyup.enter.native="getActNoInfo('payerActNo')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-row>
              <el-button type="primary" icon="el-icon-search" plain @click="openPayerDialog('payerActno')">
                账号查找
              </el-button>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="付款人名称" prop="payerName">
            <el-input v-model="form.payerName" placeholder="付款人名称"/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="付款人地址" prop="payerAddr">
            <el-input v-model="form.payerAddr" placeholder="付款人地址"/>
          </el-form-item>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-payer">
              <el-form-item label="付款行行号" prop="payerBrno">
                <acct-no-input
                  disabled
                  v-model="form.payerBrno"
                  placeholder="付款行行号"
                  @keyup.enter.native="getBankInf('payerBrno', form.payerBrno)"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="3">
            <el-button type="primary" disabled icon="el-icon-search" plain @click="openBankBranchDialog('payerBrno')">
              行号查找
            </el-button>
          </el-col>
          <el-col :span="13">
            <el-form-item label="付款行行名" prop="payerBrname" label-width="130px">
              <el-input v-model="form.payerBrname" disabled placeholder="付款行行名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-payer">
              <el-form-item label="付款人开户行号" prop="payerAccBrno">
                <acct-no-input
                  v-model="form.payerAccBrno"
                  placeholder="付款人开户行行号"
                  @keyup.enter.native="getBankInf('payerAccBrno', form.payerAccBrno)"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="3">
            <div class="grid-payer">
              <el-button type="primary" icon="el-icon-search" plain @click="openBankBranchDialog('payerAccBrno')">
                行号查找
              </el-button>
            </div>
          </el-col>
          <el-col :span="13">
            <el-form-item label="付款人开户行行名" prop="payerAccBrname" label-width="130px">
              <el-input v-model="form.payerAccBrname" placeholder="付款人开户行行名"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

      <!--收款人信息-->
      <el-card class="form-card payeeInfo-card">
        <div slot="header" class="label-header-msg">
          <span>收款人信息</span>
        </div>
        <el-row :gutter="10">
          <el-col :span="12">
            <div class="grid-payee">
              <el-form-item label="收款人账号" prop="payeeActno">
                <acct-no-input
                  v-model="form.payeeActno"
                  placeholder="收款人账号"
                  @keyup.enter.native="getActNoInfo('payeeActNo')"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-payee">
              <el-button type="primary" icon="el-icon-search" plain @click="openPayeeDialog('payeeActno')">
                账号查找
              </el-button>
            </div>
          </el-col>
        </el-row>
        <el-form-item label="收款人名称" prop="payeeName">
          <el-input v-model="form.payeeName" placeholder="收款人名称"/>
        </el-form-item>
        <el-form-item label="收款人地址" prop="payeeAddr">
          <el-input v-model="form.payeeAddr" placeholder="收款人地址"/>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-payee">
              <el-form-item label="收款行行号" prop="payeeBrno">
                <acct-no-input
                  v-model="form.payeeBrno"
                  placeholder="收款行行号"
                  :readonly="form.bizTypeCode === 'A202'"
                  @keyup.enter.native="getBankInf('payeeBrno', form.payeeBrno)"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="3">
            <div class="grid-payee">
              <el-button
                type="primary"
                icon="el-icon-search"
                plain
                :disabled="form.bizTypeCode === 'A202'"
                @click="openBankBranchDialog('payeeBrno')"
              >
                行号查找
              </el-button>
            </div>
          </el-col>
          <el-col :span="13">
            <el-form-item label="收款行行名" prop="payeeBrname" label-width="130px">
              <el-input v-model="form.payeeBrname" placeholder="收款行行名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-payee">
              <el-form-item label="收款人开户行号" prop="payeeAccBrno">
                <acct-no-input
                  v-model="form.payeeAccBrno"
                  placeholder="收款人开户行行号"
                  @keyup.enter.native="getBankInf('payeeAccBrno', form.payeeAccBrno)"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="3">
            <div class="grid-payee">
              <el-button type="primary" icon="el-icon-search" plain @click="openBankBranchDialog('payeeAccBrno')">
                行号查找
              </el-button>
            </div>
          </el-col>
          <el-col :span="13">
            <el-form-item label="收款人开户行行名" prop="payeeAccBrname" label-width="130px">
              <el-input v-model="form.payeeAccBrname" placeholder="收款人开户行行名"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

      <!--其他信息-->
      <el-card class="form-card payOtherInfo-card">
        <div slot="header" class="label-header-msg">
          <span>其他交易信息</span>
        </div>
        <el-row type="flex" :gutter="10">
          <el-col :span="11">
            <el-form-item label="交易货币" prop="curcd">
              <el-input v-model="form.curcd" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="1"></el-col>
          <el-col :span="12">
            <el-form-item label="金额" prop="amount" label-width="130px">
              <ht-amount-input v-model="form.amount" :disable="isamountdisable"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="grid-otherInfo">
              <el-form-item label="附言" prop="remarks">
                <el-input type="textarea" v-model="form.remarks" placeholder="附言" maxlength="256" show-word-limit/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-card>
      <!--附加信息域 根据业务类型，动态显示或隐藏-->
      <el-card class="form-card payAddInfo-card" v-if="this.form.bizTypeCode === 'A104'">
        <div slot="header" class="label-header-msg">
          <span>国库资金贷记划拨附加数据</span>
        </div>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="上报国库代码" prop="reportCode" label-width="110px">
                <acct-no-input v-model="form.reportCode" placeholder="上报国库代码"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="接收国库代码" prop="receiveCode" label-width="110px">
                <acct-no-input v-model="form.receiveCode" placeholder="接收国库代码"/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="报表日期" prop="reportDate" label-width="110px">
                <el-date-picker
                  v-model="form.reportDate"
                  type="date"
                  placeholder="选择日期"
                  style="width: 100%"
                  value-format="yyyyMMdd"
                ></el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <!--当业务类型为委托收款（划回）、托收承付（划回）时，“端到端标识号”中填写的“票据号码/凭证号码”规则为只填写16位票据号码-->
              <el-form-item label="报表序号" prop="reportNo" label-width="110px">
                <acct-no-input v-model="form.reportNo" placeholder="报表序号"/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预算级次" prop="budgetLevel">
              <el-select v-model="form.budgetLevel" placeholder="预算级次">
                <el-option
                  v-for="dict in dictMap.BUDGETLEVELCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调整期标志" prop="indicatorCode">
              <el-select v-model="form.indicatorCode" placeholder="调整期标志">
                <el-option
                  v-for="dict in dictMap.INDICATOR_CODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预算种类" prop="budgetType">
              <el-select v-model="form.budgetType" placeholder="预算种类">
                <el-option
                  v-for="dict in dictMap.BUDGETTYPECODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="form-card payAddInfo-card" v-if="this.form.bizTypeCode === 'A104'">
        <div slot="header" class="label-header-msg">
          <span>明细列表</span>
        </div>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="明细条数" prop="dtlCount" label-width="110px">
                <el-input v-model="form.dtlCount" placeholder="明细条数" disabled/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="明细汇总金额币种" prop="dtlCurcd" label-width="110px">
                <el-input v-model="form.dtlCurcd" placeholder="明细汇总金额币种" disabled/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="明细汇总金额" prop="dtlAmount" label-width="110px">
                <el-input v-model="form.dtlAmount" placeholder="明细汇总金额" disabled/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <div class="discountReceipt-pick-operate-box">
          <div class="operate-box-bottom-paging">
            <div class="bottom-content">
              <ht-table
                ref="dataTable104"
                :showSerialNumber="false"
                :multi-select="false"
                :opButtonList="opButtonList"
                :queryParams="queryParams"
                :tableBindColumns="tableBindColumns1"
                :showRightToolbar="false"
                :randomRowKey="true"
                :showPagination="false"
              >
                <template v-slot:operate>
                  <el-table-column label="操作" align="center" width="120" fixed="right"
                                   class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                      <ht-button-group
                        :buttonList="[
                        {
                          label: '维护',
                          // permission: 'user:xxx:xxx',
                          doFunc: () => {
                            handleAction('maintain', scope.row, scope.$index);
                          },
                        },
                        {
                          label: '删除',
                          // permission: 'user:xxx:xxx',
                          doFunc: () => {
                            handleAction('remove', scope.row, scope.$index);
                          },
                        },
                      ]"
                      />
                    </template>
                  </el-table-column>
                </template>
              </ht-table>
            </div>
          </div>
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange104"
          @current-change="handleCurrentChange104"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="->,total, sizes, prev, pager, next, jumper"
          :total="detailList104.length">
        </el-pagination>
      </el-card>

      <el-card class="form-card payAddInfo-card" v-if="this.form.bizTypeCode === 'A307'">
        <div slot="header" class="label-header-msg">
          <span>国库资金国债兑付贷记划拨附加数据</span>
        </div>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="上报国库代码" prop="reportCode" label-width="110px">
                <acct-no-input v-model="form.reportCode" placeholder="上报国库代码"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="接收国库代码" prop="receiveCode" label-width="110px">
                <acct-no-input v-model="form.receiveCode" placeholder="接收国库代码"/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="报表日期" prop="reportDate" label-width="110px">
                <el-date-picker
                  v-model="form.reportDate"
                  type="date"
                  placeholder="选择日期"
                  style="width: 100%"
                  value-format="yyyyMMdd"
                ></el-date-picker>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <!--当业务类型为委托收款（划回）、托收承付（划回）时，“端到端标识号”中填写的“票据号码/凭证号码”规则为只填写16位票据号码-->
              <el-form-item label="报表序号" prop="reportNo" label-width="110px">
                <acct-no-input v-model="form.reportNo" placeholder="报表序号"/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="form-card payAddInfo-card" v-if="this.form.bizTypeCode === 'A307'">
        <div slot="header" class="label-header-msg">
          <span>明细列表</span>
        </div>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="明细条数" prop="dtlCount01" label-width="110px">
                <el-input v-model="form.dtlCount01" placeholder="明细条数" disabled/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="明细汇总金额币种" prop="dtlCurcd" label-width="110px">
                <el-input v-model="form.dtlCurcd" placeholder="明细汇总金额币种" disabled/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-payAddInfo">
              <el-form-item label="明细汇总金额" prop="dtlAmount_A307" label-width="110px">
                <ht-amount-input v-model="form.dtlAmount_A307" disable placeholder="明细汇总金额" disabled/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      <div class="discountReceipt-pick-operate-box">
        <div class="operate-box-bottom-paging">
          <div class="bottom-content">
            <ht-table
              ref="dataTable307"
              :showSerialNumber="false"
              :multi-select="false"
              :opButtonList="opButtonList"
              :queryParams="queryParams"
              :tableBindColumns="tableBindColumns2"
              :showRightToolbar="false"
              :randomRowKey="true"
              :showPagination="false"
            >
              <template v-slot:operate>
                <el-table-column label="操作" align="center" width="120" fixed="right" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <ht-button-group
                      :buttonList="[
                        {
                          label: '维护',
                          // permission: 'user:xxx:xxx',
                          doFunc: () => {
                            handleAction('maintain', scope.row, scope.$index);
                          },
                        },
                        {
                          label: '删除',
                          // permission: 'user:xxx:xxx',
                          doFunc: () => {
                            handleAction('remove', scope.row, scope.$index);
                          },
                        },
                      ]"
                    />
                  </template>
                </el-table-column>
              </template>
            </ht-table>
          </div>
        </div>
      </div>
        <el-pagination
          background
          @size-change="handleSizeChange307"
          @current-change="handleCurrentChange307"
          :current-page="currentPage307"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize307"
          layout="->,total, sizes, prev, pager, next, jumper"
          :total="detailList307.length">
        </el-pagination>
      </el-card>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm(null)">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
      <el-button v-if="ifShowDelBtn" type="danger" @click="delTran">删 除</el-button>
    </div>
    <bank-branch-dialog ref="bankBranchDialog" @confirm="confirmBankBranch"></bank-branch-dialog>
    <payee-dialog ref="payeeDialog" @confirm="confirmPayee"></payee-dialog>
    <payer-dialog ref="payerDialog" @confirm="confirmPayer"></payer-dialog>
    <table-104-dialog
      ref="table104Dialog"
      :dictMap="dictMap"
      :formData.sync="formData"
      @saveComplete104="saveComplete104"
    ></table-104-dialog>
    <table-307-dialog
      ref="table307Dialog"
      :formData.sync="formData"
      :dictMap="dictMap"
      @saveComplete307="saveComplete307"
    ></table-307-dialog>
  </el-dialog>
</template>
<script>
import {
  addTxn,
  bepsFinancialOrdinaryEntry,
  bepsFinancialOrdinaryUpd,
  updTxn,
} from "@/api/cnaps/baseBiz/send/beps/beps.js";
import {
  apiUrlConfigA104,
  apiUrlConfigA307,
  delTxn,
  detail,
  detailByCode
} from "@/api/cnaps/baseBiz/send/sendCommon.js";
import {getOtherCustByActno, getCustByActno} from "@/api/cnaps/common/custInfo";
import {getBankInfByBankCode} from "@/api/cnaps/common/bankInfo";
import {isLessThenHvpsMinLimitNew} from "@/api/cnaps/common/cp2SysParam";
import BankBranchDialog from "@/views/components/BankBranchDialog";
import PayeeDialog from "@/views/components/PayeeDialog";
import PayerDialog from "@/views/components/PayerDialog";
import Table104Dialog from "./Table104Dialog.vue";
import Table307Dialog from "./Table307Dialog.vue";
import HtAmountInput from "@/views/components/HtAmountInput";
import HtAmountUpper from "@/views/components/HtAmountUpper";
import AcctNoInput from "@/views/components/AcctNoInput/index.vue";
import {config} from "process";
import {deflate} from "zlib";
import {mapGetters} from "vuex";

export default {
  name: "EditInfo",
  components: {
    PayeeDialog,
    PayerDialog,
    BankBranchDialog,
    Table104Dialog,
    Table307Dialog,
    HtAmountInput,
    HtAmountUpper,
    AcctNoInput
  },
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      currentPage: 1,
      currentPage307: 1,
      pageSize: 20,
      pageSize307: 20,
      detailList104: [],
      detailList307: [],
      reqConfig307: apiUrlConfigA307,
      reqConfig104: apiUrlConfigA104,
      show104: true,
      show307: false,
      isamountdisable: true,
      tableBindColumns1: [
        {label: `征收机关大类代码`, prop: "deptCode", width: "280", align: "center", dictCode: "DEPT_CODE"},
        {
          label: `预算科目代码`,
          prop: "subjectCode",
          width: "280",
          align: "center",
        },
        {
          label: `发生额`,
          prop: "incurredAmount",
          width: "280",
          align: "center",
        },
      ],
      tableBindColumns2: [
        {
          label: `兑付国债银行大类`,
          prop: "debtBankType",
          width: "200",
          align: "center",
          dictCode: "DEBT_BANK_TYPE",
        },
        {
          label: `本金代码`,
          prop: "capitalCode",
          width: "200",
          align: "center",
        },
        {
          label: `本金金额`,
          prop: "capitalAmount",
          width: "200",
          align: "center",
          type: "price",
        },
        {
          label: `利息代码`,
          prop: "accrualCode",
          width: "200",
          align: "center",
        },
        {
          label: `利息金额`,
          prop: "accrualAmount",
          width: "200",
          align: "center",
          type: "price",
        },
      ],
      opButtonList: () => {
        return [
          {
            label: "新增",
            type: "primary",
            icon: "el-icon-plus",
            permission: "user:expandConfInfo:add",
            actionType: "add",
            clickFun: () => {
              this.addRow();
            },
          },
        ];
      },
      busTypeList: [],
      id: "",
      title: "",
      pkgNo: "",
      // 是否显示弹出层
      openState: false,
      ifShowDelBtn: false,
      queryParams: {
        id: ""
      },
      // 字段可编辑控制

      // 表单参数
      form: {
        listA104: [],
        listA307: [],
      },
      formData: {},
      // 表单校验
      rules: {
        bizTypeCode: [{required: true, message: "请选择业务类型", trigger: "change"}],
        bizCtgyCode: [{required: true, message: "请选择业务种类", trigger: "change"}],
        cbflag: [{required: true, message: "请选择记账标志", trigger: "change"}],
        custflag1: [{required: true, message: "请选择账户类型", trigger: "change"}],
        payerBrno: [{required: true, message: "请输入付款行行号", trigger: "blur"},
          {max: 14, message: "付款行行号最长14位", trigger: "blur"}],
        payerBrname: [{required: true, max: 60, message: "请输入付款行名", trigger: "blur"}],
        payerAccBrno: [{required: false, message: "请输入付款人开户行号", trigger: ['blur', 'change']},
          {max: 14, message: "付款人开户行号最长14位", trigger: "blur"}],
        payerAccBrname: [{max: 140, message: "收款人开户行号最长140位", trigger: "blur"}],
        payerActno: [{required: true, message: "请输入付款人账号", trigger: ['blur', 'change']},
          {max: 32, message: "付款人账号最长32位", trigger: "blur"}],
        payerName: [{required: true, message: "请输入付款人名称", trigger: ['blur', 'change']},
          {max: 60, message: "付款人名称最长60位", trigger: "blur"}],
        payerAddr: [{max: 70, message: "付款人地址最长70位", trigger: "blur"}],
        payeeActno: [{required: true, message: "请输入收款人账号", trigger: ['blur', 'change']},
          {max: 32, message: "收款人账号最长32位", trigger: "blur"}],
        payeeName: [{required: true, message: "请输入收款人名称", trigger: ['blur', 'change']},
          {max: 60, message: "收款人名称最长60位", trigger: "blur"}],
        payeeAddr: [{max: 70, message: "收款人地址最长70位", trigger: "blur"}],
        payeeBrno: [{required: true, message: "请输入收款行行号", trigger: ['blur', 'change']},
          {max: 14, message: "收款行行号最长14位", trigger: "blur"}],
        payeeBrname: [{required: true, message: "请输入收款行行名", trigger: ['blur', 'change']},
          {max: 60, message: "收款行行名最长60位", trigger: "blur"}],
        payeeAccBrno: [{required: false, message: "请输入收款人开户行号", trigger: ['blur', 'change']},
          {max: 14, message: "收款人开户行号最长14位", trigger: "blur"}],
        payeeAccBrname: [{max: 140, message: "收款人开户行名最长140位", trigger: "blur"}],
        reportCode: [{required: true, message: "请输入上报国库代码", trigger: "blur"},
          {max: 10, message: "上报国库代码最长10位", trigger: "blur"}],
        receiveCode: [{required: true, message: "请输入接收国库代码", trigger: "blur"},
          {max: 10, message: "接收国库代码最长10位", trigger: "blur"}],
        reportDate: [{required: true, message: "请选择报表日期", trigger: "change"}],
        reportNo: [{required: true, message: "请输入报表序号", trigger: "blur"},
          {max: 10, message: "报表序号最长10位", trigger: "blur"}],
        budgetLevel: [{required: true, message: "请选择预算级次", trigger: "change"}],
        indicatorCode: [{required: true, message: "请选择调整期标志", trigger: "change"}],
        budgetType: [{required: true, message: "请选择预算种类", trigger: "change"}],
        amount: [{required: true, message: "请输入金额", trigger: "blur"}],
      },
    };
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["globalBusData"]),
  },
  methods: {
    //切换每页显示数据量时的回调
    handleSizeChange104 (size) {
      this.pageSize = size;
      this.handleCurrentChange104(this.currentPage);
    },
    handleSizeChange307 (size) {
      this.pageSize307 = size;
      this.handleCurrentChange307(this.currentPage307);
    },
    //切换页码回调
    handleCurrentChange104 (currentPage) {
      let page = this.detailList104.slice((currentPage-1)*this.pageSize,currentPage*this.pageSize);
      this.$refs.dataTable104.dataList = page
      this.currentPage = currentPage;
    },
    handleCurrentChange307 (currentPage) {
      let page = this.detailList307.slice((currentPage-1)*this.pageSize307,currentPage*this.pageSize307);
      this.$refs.dataTable307.dataList = page
      this.currentPage307 = currentPage;
    },

    // 表单重置
    reset() {
      this.resetForm("editForm");
      this.$set(this, "form", {});
      this.detailList104 = [];
      this.detailList307 = [];
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.reset();
          done();
        })
        .catch((_) => {
        });
    },

    bizTypeCodeChange(code) {
      if (code === 'A118') {
        this.isamountdisable = false;
      } else {
        this.isamountdisable = true;
      }
      //联动修改bizCtgyCode
      let dictKeyPre = `BIZ_CTGY_BEPS_${code}`;
      this.getDicts(dictKeyPre).then((response) => {
        this.busTypeList = response.result;
        this.$set(this.form, "bizCtgyCode", this.busTypeList[0].value);
      });

      this.form.dtlCount = 0;
      this.form.dtlCount01 = 0;
      this.form.curcd = "CNY";
      this.form.amount = 0.0;
      this.form.dtlCurcd = "CNY";
      this.form.dtlAmount = 0.0;
      this.form.dtlAmount_A307 = 0.0;

    },
    bizCtgyCodeChange() {
      //业务种类变化，城商行商业汇票特殊处理
      //城市商业银行汇票资金移存”时，“付款人开户行行号、付款人账号、付款人名称”必填，
      //“收款人开户行行号、收款人账号、收款人名称”可选填
      //城市商业银行汇票资金清算、城市商业银行汇票资金多余划回、城市商业银行汇票资金未用退回”时，
      //“收款人开户行行号、收款人账号、收款人名称”必填，
      //“付款人开户行行号、付款人账号、付款人名称”可选填；
      if (this.form.bizTypeCode == "A202") {
        switch (this.form.bizCtgyCode) {
          case "02901": {
            this.$set(this.rules.payeeActno[0], "required", false);
            this.$set(this.rules.payeeName[0], "required", false);
            this.$set(this.rules.payeeAccBrno[0], "required", false);
            this.$set(this.rules.payerActno[0], "required", true);
            this.$set(this.rules.payerName[0], "required", true);
            this.$set(this.rules.payerAccBrno[0], "required", true);
            break;
          }
          case "02902":
          case "02903":
          case "02904": {
            this.$set(this.rules.payerActno[0], "required", false);
            this.$set(this.rules.payerName[0], "required", false);
            this.$set(this.rules.payerAccBrno[0], "required", false);
            this.$set(this.rules.payeeActno[0], "required", true);
            this.$set(this.rules.payeeName[0], "required", true);
            this.$set(this.rules.payeeAccBrno[0], "required", true);
            break;
          }
          default: {
            this.$set(this.rules.payerActno[0], "required", true);
            this.$set(this.rules.payerName[0], "required", true);
            this.$set(this.rules.payerAccBrno[0], "required", true);
            this.$set(this.rules.payeeActno[0], "required", true);
            this.$set(this.rules.payeeName[0], "required", true);
            this.$set(this.rules.payeeAccBrno[0], "required", true);
            break;
          }
        }
      }
    },
    // 回车查证账号信息
    getActNoInfo(actNoType) {
      this.form.userInfo = {brno: "676"};
      if (actNoType === "payeeActNo") {
        var actNo = this.form.payeeActno;
        getOtherCustByActno(actNo).then((response) => {
          if (response.code == "S" && response.result !== null) {
            this.$set(this.form, "payeeActno", response.result.actNo);
            this.$set(this.form, "payeeName", response.result.name);
            this.$set(this.form, "payeeBrno", response.result.rcvBrno);
            this.$set(this.form, "payeen", response.result.rcvBrname);
            this.$set(this.form, "payeeAddr", response.result.addr);
            this.$set(this.form, "payeeAccBrno", response.result.accBrno);
            this.$set(this.form, "payeeAccBrname", response.result.accBrnoName);
          } else {
            this.msgSuccess("无历史付款记录");
            this.$set(this.form, "payeeName", null);
            this.$set(this.form, "payeeBrno", null);
            this.$set(this.form, "payeeBrname", null);
            this.$set(this.form, "payeeAddr", null);
            this.$set(this.form, "payeeAccBrno", null);
            this.$set(this.form, "payeeAccBrname", null);
          }
        });
      } else {
        var actNo = this.form.payerActno;
        getCustByActno(actNo).then((response) => {
          if (response.code == "S" && response.result !== null) {
            this.$set(this.form, "payerActno", response.result.actNo);
            this.$set(this.form, "payerName", response.result.name);
            this.$set(this.form, "payerAddr", response.result.addr);
          } else {
            this.msgSuccess("查无此账号");
            this.$set(this.form, "payerName", null);
            this.$set(this.form, "payerAddr", null);
          }
        });
      }
    },
    // 检查大额交易金额上限
    checkAmountLimit() {
      isLessThenHvpsMinLimitNew(this.form.bizTypeCode, this.form.bizCtgyCode, this.form.bizPriority, this.form.amount);
    },

    openBankBranchDialog(fromFlag = "") {
      this.$refs.bankBranchDialog.show(fromFlag);
    },
    confirmBankBranch(fromFlag, row) {
      if (fromFlag == "payeeBrno") {
        this.$set(this.form, "payeeBrno", row.bankCode);
        this.$set(this.form, "payeeBrname", row.orgFullname);
      } else if (fromFlag == "payerAccBrno") {
        this.$set(this.form, "payerAccBrno", row.bankCode);
        this.$set(this.form, "payerAccBrname", row.orgFullname);
      } else if (fromFlag == "payerBrno") {
        this.$set(this.form, "payerBrno", row.bankCode);
        this.$set(this.form, "payerBrname", row.orgFullname);
      } else if (fromFlag == "payeeAccBrno") {
        this.$set(this.form, "payeeAccBrno", row.bankCode);
        this.$set(this.form, "payeeAccBrname", row.orgFullname);
      }
    },
    //回车取行号信息
    getBankInf(searchType, bankCode) {
      getBankInfByBankCode(bankCode).then((response) => {
        if (response.code == "S" && response.result !== null) {
          switch (searchType) {
            case "payerBrno":
              this.$set(this.form, "payerBrno", response.result.bankCode);
              this.$set(this.form, "payerBrname", response.result.orgFullname);
              break;
            case "payerAccBrno":
              this.$set(this.form, "payerAccBrno", response.result.bankCode);
              this.$set(this.form, "payerAccBrname", response.result.orgFullname);
              break;
            case "payeeBrno":
              this.$set(this.form, "payeeBrno", response.result.bankCode);
              this.$set(this.form, "payeeBrname", response.result.orgFullname);
              break;
            case "payeeAccBrno":
              this.$set(this.form, "payeeAccBrno", response.result.bankCode);
              this.$set(this.form, "payeeAccBrname", response.result.orgFullname);
              break;
          }
        } else {
          this.msgSuccess("查无此行号");
          switch (searchType) {
            case "payerBrno":
              this.$set(this.form, "payerBrname", null);
              break;
            case "payerAccBrno":
              this.$set(this.form, "payerAccBrname", null);
              break;
            case "payeeBrno":
              this.$set(this.form, "payeeBrname", null);
              break;
            case "payeeAccBrno":
              this.$set(this.form, "payeeAccBrname", null);
              break;
          }
        }
      });
    },
    openPayeeDialog(fromFlag = "") {
      this.$refs.payeeDialog.show(fromFlag);
    },
    confirmPayee(fromFlag, row) {
      if (fromFlag == "payeeActno") {
        this.$set(this.form, "payeeActno", row.actNo);
        this.$set(this.form, "payeeName", row.name);
        this.$set(this.form, "payeeBrno", row.rcvBrno);
        this.$set(this.form, "payeeBrname", row.rcvBrname);
        this.$set(this.form, "payeeAddr", row.addr);
        this.$set(this.form, "payeeAccBrno", row.accBrno);
        this.$set(this.form, "payeeAccBrname", row.accBrnoName);
      }
    },
    openPayerDialog(fromFlag = "") {
      this.$refs.payerDialog.show(fromFlag);
    },
    confirmPayer(fromFlag, row) {
      if (fromFlag == "payerActno") {
        this.$set(this.form, "payerActno", row.actNo);
        this.$set(this.form, "payerName", row.name);
        this.$set(this.form, "payerAddr", row.addr);
      }
    },
    show(id = "", pkgNo = "") {
      this.reset();
      this.pkgNo = pkgNo;
      this.id = id;
      this.form.dtlCount = 0;
      this.form.dtlCount01 = 0;
      this.form.curcd = "CNY";
      this.form.amount = 0.0;
      this.form.dtlCurcd = "CNY";
      this.form.dtlAmount = 0.0;
      this.form.dtlAmount_A307 = 0.0;
      this.title = "小额金融机构发起贷记录入";
      this.openState = true;
      this.ifShowDelBtn = false;


      // 初始化选择
      this.$set(this.form, "bizTypeCode", this.dictMap.BIZ_TYPE_122[0].value);
      this.bizTypeCodeChange(this.form.bizTypeCode);
      this.$set(this.form, "payerBrno", this.globalBusData.cnapsBrno); //付款行号初始化当前操作员所以机构
      this.$set(this.form, "payerBrname", this.globalBusData.cnapsBrname);
      this.$set(this.form, "payerAccBrno", this.globalBusData.cnapsBrno);
      this.$set(this.form, "payerAccBrname", this.globalBusData.cnapsBrname);
      this.$set(this.form, "custflag1", this.dictMap.CUST_FLAG1[0].value);
      this.$set(this.form, "cbflag", this.dictMap.CBFLAG[0].value);
      if (id) {
        this.title = "小额金融机构发起贷记修改";
        this.ifShowDelBtn = true;

        // 如果传了ID 这里查询一个已经存在的数据
        detail({
          coreId: this.id,
          pkgNo: this.pkgNo,
        }).then((res) => {
          this.$set(this, "form", res.result.detail6410DataModel);
          console.log(this.form.id)
          // this.$set(this.queryParams, "id", this.form.id);
          //明细列表查询(修改用)
          if (this.form.bizTypeCode == "A104") {
            detailByCode({id: this.form.id, code: "A104"}
              ).then(res =>{
              this.$refs.dataTable104.dataList = res.result;
              this.detailList104= res.result;
              this.$set(this.form, "dtlCount", this.detailList104.length);
            })
          } else if (this.form.bizTypeCode == "A307") {
            detailByCode({id: this.form.id, code: "A307"}
            ).then(res =>{
              this.$refs.dataTable307.dataList = res.result;
              this.detailList307= res.result;
              this.$set(this.form, "dtlAmount_A307", this.form.dtlAmount);
              this.$set(this.form, "dtlCount01", this.detailList307.length);
            })
          }
        });
      }
    },
    addRow() {

      if (this.form.bizTypeCode == "A104") {
        this.formData.isUpdate = false;

        this.formData.deptCode = this.dictMap.DEPT_CODE[0].value;
        this.formData.curcd = "CNY";
        this.$refs.table104Dialog.show();
      }
      if (this.form.bizTypeCode == "A307") {
        this.formData.isUpdate = false;
        this.formData.debtBankType = this.dictMap.DEBT_BANK_TYPE[0].value;
        this.formData.curcd = "CNY";
        this.formData.acccurcd = "CNY";
        this.$refs.table307Dialog.show();
      }
    },
    /** 明细按钮操作 */
    handleAction(type, row, index) {
      switch (type) {
        case "maintain":
          this.formData = _.clone(row);
          this.formData.isUpdate = true;
          this.formData.index = index;
          if (this.form.bizTypeCode == "A104") {
            this.formData.curcd = "CNY";
            this.$refs.table104Dialog.show();
          }
          if (this.form.bizTypeCode == "A307") {
            this.formData.curcd = "CNY";
            this.formData.acccurcd = "CNY";
            this.$refs.table307Dialog.show();
          }
          break;
        case "remove":
          // 删除
          if (this.form.bizTypeCode == "A104") {
            this.$confirm("确定删除该条明细清单信息吗?", "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              let delIndex = (this.currentPage-1) * this.pageSize + index;
              this.detailList104.splice(delIndex, 1);
              this.$refs.dataTable104.dataList = this.detailList104
              if(index === 0 && this.currentPage !== 1){
                this.handleCurrentChange104(this.currentPage-1);
              }else {
                this.handleCurrentChange104(this.currentPage);
              }
              let sum = 0;
              this.detailList104.map((res) => {
                return (sum += Number(res.incurredAmount));
              });
              this.$set(this.form, "dtlCount", this.detailList104.length);
              this.$set(this.form, "dtlAmount", sum);
            });
          }
          if (this.form.bizTypeCode == "A307") {
            this.$confirm("确定删除该条明细清单信息吗?", "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              let delIndex307 = (this.currentPage307-1) * this.pageSize307 + index;
              this.detailList307.splice(delIndex307, 1);
              this.$refs.dataTable307.dataList = this.detailList307
              if(index === 0 && this.currentPage307 !== 1){
                this.handleCurrentChange307(this.currentPage307-1);
              }else {
                this.handleCurrentChange307(this.currentPage307);
              }
              let sum = 0;
              this.detailList307.map((res) => {
                return (sum += Number(res.capitalAmount) + Number(res.accrualAmount));
              });
              this.$set(this.form, "dtlCount01", this.detailList307.length);
              this.$set(this.form, "dtlAmount_A307", sum);
            });
          }
          break;
      }
    },
    saveComplete104(val) {
      let formdata104 = JSON.parse(JSON.stringify(val));
      console.log(formdata104)
      if (val.isUpdate) {
        this.$set(this.detailList104, (this.currentPage-1) * this.pageSize + val.index, formdata104);
        this.$refs.dataTable104.dataList = this.detailList104
        this.handleCurrentChange104(this.currentPage);
        this.$nextTick(() => {
          let sum = 0;
          this.detailList104.map((res) => {
            return (sum += Number(res.incurredAmount));
          });
          this.$delete(this.form, "dtlCount");
          this.$set(this.form, "dtlCount", this.detailList104.length);
          this.$set(this.form, "dtlAmount", sum);
          this.$set(this.form, "amount", sum);
        });
      } else {
        this.$refs.dataTable104.dataList.push(formdata104);
        this.detailList104.push(formdata104)
        this.$nextTick(() => {
          let sum = 0;
          this.detailList104.map((res) => {
            return (sum += Number(res.incurredAmount));
          });
          this.$delete(this.form, "dtlCount");
          this.$set(this.form, "dtlCount", this.detailList104.length);
          this.$set(this.form, "dtlAmount", sum);
          this.$set(this.form, "amount", sum);
          // this.$refs.dataTable104.doQuery(true);
        });
        this.handleCurrentChange104(this.currentPage);
      }
      this.$set(this.form, "listA104", this.$refs.dataTable104.dataList)

    },
    saveComplete307(val) {
      let formdata307 = JSON.parse(JSON.stringify(val));
      console.log(formdata307)
      if (val.isUpdate) {
        this.$set(this.detailList307, (this.currentPage307-1) * this.pageSize307 + val.index, formdata307);
        this.$refs.dataTable307.dataList = this.detailList307
        this.handleCurrentChange307(this.currentPage307);
        this.$nextTick(() => {
          let sum = 0;
          this.detailList307.map((res) => {
            return (sum += Number(res.capitalAmount) + Number(res.accrualAmount));
          });
          this.$delete(this.form, "dtlCount01");
          this.$set(this.form, "dtlCount01", this.detailList307.length);
          this.$set(this.form, "dtlAmount_A307", sum);
          this.$set(this.form, "amount", sum);
        });
      } else {
        this.$refs.dataTable307.dataList.push(formdata307);
        this.detailList307.push(formdata307)
        this.$nextTick(() => {
          let sum = 0;
          this.detailList307.map((res) => {
            return (sum += Number(res.capitalAmount) + Number(res.accrualAmount));
          });
          this.$delete(this.form, "dtlCount01");
          this.$set(this.form, "dtlCount01", this.detailList307.length);
          this.$set(this.form, "dtlAmount_A307", sum);
          this.$set(this.form, "amount", sum);
        });
        this.handleCurrentChange307(this.currentPage307);
      }
      this.$set(this.form, "listA307", this.$refs.dataTable307.dataList)
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", false);
    },
    submitCheck(res) {
      if (res.code === "C") {
        this.$confirm(res.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.submitForm(res.result);
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消执行'
          });
          return;
        });
      }
    },
    /** TODO提交按钮 */
    submitForm: function (pass) {
      this.$refs["editForm"].validate((valid) => {
        this.form.pass = pass;
        if (this.form.bizTypeCode === 'A307') {
          this.$set(this.form, "dtlCount", this.form.dtlCount01);
          this.$set(this.form, "dtlAmount", this.form.dtlAmount_A307);
        }
        this.$set(this.form, "listA104", this.detailList104)
        this.$set(this.form, "listA307", this.detailList307)
        if (valid) {
          //如果有ID 则是修改
          if (this.id) {
            bepsFinancialOrdinaryUpd(this.form).then((res) => {
              this.submitCheck(res);
              if (res.code === "S") {
                this.msgSuccess("修改成功");
                this.$emit("editComplete", true);
                this.openState = false;
              }
            });
          } else {
            bepsFinancialOrdinaryEntry(this.form).then((res) => {
              this.submitCheck(res);
              if (res.code === "S") {
                this.msgSuccess("新增成功");
                this.$alert(`CORE_ID: ${res.result.rspId}`, "生成交易流水号", {
                  confirmButtonText: "确定",
                  //刷新一下查询列表 TODO
                  callback: (action) => {
                  },
                });
                this.$emit("editComplete", true);
                this.openState = false;
              }
            });
          }
        }
      });
    },
    /** 删除 按钮 */
    delTran: function () {
      if (this.id) {
        this.$confirm("确认删除吗？", "交易删除提示", {type: "warning"}).then((_) => {
          delTxn({id: this.id}).then((res) => {
            this.msgSuccess("删除成功");

            this.$emit("editComplete", true);
            this.openState = false;
          });
        });
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
